package cn.samehope.pet.task;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import cn.samehope.pet.model.PetAnimal;
import cn.samehope.pet.web.animal.PetAnimalService;

import com.jfinal.kit.PathKit;
import com.jfinal.weixin.sdk.api.MediaApi;
import com.jfinal.weixin.sdk.api.MediaFile;


public class WxPic {
	
	public void downFile() throws IOException{
		PetAnimalService srv = PetAnimalService.me;
		List<PetAnimal> list = srv.findAll();
		for(PetAnimal pa : list){
			MediaFile mediaFile = MediaApi.getMedia(pa.getPic());
			BufferedInputStream in = mediaFile.getFileStream();
			BufferedOutputStream out = new BufferedOutputStream(
					new FileOutputStream(
							PathKit.getWebRootPath() + File.separator + "download" + File.separator + mediaFile.getFullName()
							)
					);
			int i;
			while((i=in.read())!=-1){
			   out.write(i);
			}
			out.flush();
			out.close();
			in.close();
		}
	}
}
